package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentRatingDataBean;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

public class ComponentRatingProvider extends RecyclerAdapter<ComponentRatingProvider.ViewHolder> {

    private List<ComponentRatingDataBean> mList;
    private Context context;


    public ComponentRatingProvider(List<ComponentRatingDataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_component_rating_provider, componentContainer, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ViewHolder viewHolder = holder;

        if (mList != null ) {
            if (position == 0 || position==5 || position==9 || position==14){
                viewHolder.on_text.setMultipleLine(true);
                viewHolder.on_text.setText(mList.get(position).getOn_text());
                viewHolder.on_text.setTextColor(Color.GRAY);
                //viewHolder.on_text.setTextSize(38);
                viewHolder.left_img.setVisibility(Component.HIDE);
                viewHolder.left_text.setVisibility(Component.HIDE);
                viewHolder.right_text.setVisibility(Component.HIDE);
                viewHolder.under_text.setVisibility(Component.HIDE);
                viewHolder.rating1.setVisibility(Component.HIDE);
                viewHolder.rating2.setVisibility(Component.HIDE);
                viewHolder.rating3.setVisibility(Component.HIDE);
                viewHolder.rating4.setVisibility(Component.HIDE);
                viewHolder.rating5.setVisibility(Component.HIDE);
                viewHolder.on_text.setPaddingLeft(130);
                viewHolder.on_text.setPaddingRight(110);
                viewHolder.on_text.setMarginTop(60);
            }

            viewHolder.left_img.setImageAndDecodeBounds(mList.get(position).getLeft_img());
            viewHolder.left_text.setText(mList.get(position).getLeft_text());
            viewHolder.on_text.setText(mList.get(position).getOn_text());
            viewHolder.under_text.setText(mList.get(position).getUnder_text());
            viewHolder.right_text.setText(mList.get(position).getRight_text());
            viewHolder.under_text.setTextColor(Color.GRAY);

            Element rating1 = mList.get(position).getRating1();
            Element rating2 = mList.get(position).getRating2();
            Element rating3 = mList.get(position).getRating3();
            Element rating4 = mList.get(position).getRating4();
            Element rating5 = mList.get(position).getRating5();
            viewHolder.rating1.setImageElement(rating1);
            viewHolder.rating2.setImageElement(rating2);
            viewHolder.rating3.setImageElement(rating3);
            viewHolder.rating4.setImageElement(rating4);
            viewHolder.rating5.setImageElement(rating5);
            viewHolder.left_img.setCornerRadius(100);


        }

    }


    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int positon) {
        return positon;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {

        private Image left_img,rating1,rating2,rating3,rating4,rating5;
        private Text left_text,on_text,under_text,right_text;


        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            left_img = (Image) itemComponent.findComponentById(ResourceTable.Id_left_img);
            left_text = (Text) itemComponent.findComponentById(ResourceTable.Id_left_text);
            on_text = (Text) itemComponent.findComponentById(ResourceTable.Id_on_text);
            under_text = (Text) itemComponent.findComponentById(ResourceTable.Id_under_text);
            right_text = (Text) itemComponent.findComponentById(ResourceTable.Id_right_text);
            rating1 = (Image) itemComponent.findComponentById(ResourceTable.Id_rating1);
            rating2 = (Image) itemComponent.findComponentById(ResourceTable.Id_rating2);
            rating3 = (Image) itemComponent.findComponentById(ResourceTable.Id_rating3);
            rating4 = (Image) itemComponent.findComponentById(ResourceTable.Id_rating4);
            rating5 = (Image) itemComponent.findComponentById(ResourceTable.Id_rating5);

        }
    }
}
